var mergeSort = function (arr) {
    return mergeSortRec(arr);
}
var mergeSortRec=function (arr) {
    var length=arr.length;
    if(length===1){
        return arr;
    }
    var mid=Math.floor(length/2);
    var left=arr.slice(0,mid);
    var right=arr.slice(mid,length);
    return merge(mergeSortRec(left),mergeSortRec(right));
}
var merge=function (left,right) {
    var result=[];
    var il=0;
    var ir=0;
    while(il<left.length&&ir<right.length){
        if(left[il]<right[ir]){
            result.push(left[il++]);
        }else{
            result.push(right[ir++]);
        }
    }
    while(il<left.length){
        result.push(left[il++]);
    }
    while ((ir<right.length)){
        result.push(right[ir++]);
    }
    return result;
}
var arr = [1, 6, 2, 5, 9, 2, 0];
console.log(arr.toString());
console.log(mergeSort(arr).toString());